const xss = require("xss");
const { exec } = require("../db/mysql");

const paperTypeList = () => {
  let sql = `select * from grade`;
  return exec(sql);
};
const addPaperType = (paperData = {}) => {
  const grade_name = xss(paperData.grade_name);
  const sql = `insert into grade (grade_name) values('${grade_name}');`;

  return exec(sql).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updatePaperType = (id, paperData = {}) => {
  const grade_name = xss(paperData.grade_name);

  const sql = `update grade set grade_name='${grade_name}' where grade_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const delPaperType = (id) => {
  console.log(id);
  const Selsql = `select paper.grade_id from paper where grade_id=${id}`;
  return exec(Selsql).then((rows) => {
    if (rows[0] === undefined) {
      const sql = `delete from grade where grade_id=${id}`;
      return exec(sql).then((delData) => {
        if (delData.affectedRows > 0) {
          return true;
        }
        return false;
      });
    } else {
      return {
        code: -1,
        msg: "无法删除该类型",
      };
    }
  });
};
//--------------------------------------------------------------------------
const getUserCName = () => {
  let sql = `select user.user_id,user.user_name,user.user_cname from user`;
  return exec(sql);
};
//******************** */
const paperList = (isadmin) => {
  let sql = `select paper.*, user.user_name,grade.grade_name
   from paper,user,grade where paper.user_id=user.user_id and 
   paper.grade_id=grade.grade_id and paper.isadmin='${isadmin}'`;
  return exec(sql);
};
const getDetail = (id) => {
  const sql = `select * from paper where paper_id='${id}'`;
  return exec(sql).then((rows) => {
    return rows[0];
  });
};
const addPaper = (paperData = {}) => {
  let paper_title = paperData.paper_title;
  let paper_content = paperData.paper_content;
  let grade_id = paperData.grade_id;
  let paper_time = paperData.paper_time;
  let user_id = paperData.user_id;
  let isadmin = paperData.isadmin;

  const sql = `insert into paper 
  (paper_title,paper_content,grade_id,paper_time,user_id,isadmin) 
    values('${paper_title}','${paper_content}',${grade_id},'${paper_time}','${user_id}','${isadmin}');`;

  return exec(sql).then((insertData) => {
    return {
      id: insertData.insertId,
    };
  });
};
const updatePaper = (id, paperData = {}) => {
  let paper_title = paperData.paper_title;
  let paper_content = paperData.paper_content;
  let grade_id = paperData.grade_id;
  let paper_time = paperData.paper_time;

  const sql = `update paper set paper_title='${paper_title}', 
  paper_content='${paper_content}', grade_id='${grade_id}', 
  paper_time='${paper_time}' where paper_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updatePaperTop = (id, paperData = {}) => {
  let is_top = paperData.is_top;

  const sql = `update paper set is_top='${is_top}' where paper_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const delPaper = (id) => {
  console.log(id);
  const sql = `delete from paper where paper_id=${id}`;
  return exec(sql).then((delData) => {
    if (delData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};

module.exports = {
  paperTypeList,
  addPaperType,
  updatePaperType,
  delPaperType,
  //-------------
  getUserCName,
  //----------
  paperList,
  getDetail,
  addPaper,
  updatePaper,
  updatePaperTop,
  delPaper,
};
